package rsj.manager;

import com.rlyy.aibase.utils.base.MapNode;
import rsj.dao.ProdUpDao;
import rsj.dao.ProductDao;
import rsj.table.SYS_PROD;

public class ProdManager {

    private static final ProdManager instance = new ProdManager();

    public static ProdManager getInstance() {
        return instance;
    }

    private static final ProductDao productDao = ProductDao.getInstance();
    private static final ProdUpDao prodUpDao = ProdUpDao.getInstance();

    public String insertOrUpdate(MapNode data) {
        long prodId = Long.parseLong(data.getValueDef(SYS_PROD.Prod_Id, "0"));
        String New_Prod_Docid = data.getValueDef(SYS_PROD.Prod_Docid, "");
        String New_Prod_Docname = data.getValueDef(SYS_PROD.Prod_Docname, "");

        String Prod_Id = null;

        if (prodId <= 0) {// 新增数据 后结果处理
            Prod_Id = productDao.insertOrUpdate(data);
            prodUpDao.insert(Prod_Id, New_Prod_Docid, New_Prod_Docname);
        } else {// 更新数据 后结果处理
            MapNode mapNode = productDao.getById(data.getValue(SYS_PROD.Prod_Id));
            String Prod_Docid = mapNode.getValue("Prod_Docid");
            String Prod_DocName = mapNode.getValue(SYS_PROD.Prod_Docname);

            Prod_Id = productDao.insertOrUpdate(data);
            if (Prod_Docid.equals(New_Prod_Docid) && Prod_DocName.equals(New_Prod_Docname)) {// 文档没有重新上传
                return Prod_Id;
            }
            prodUpDao.insert(Prod_Id, Prod_Docid, New_Prod_Docname);
        }
        return Prod_Id;
    }
}
